<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="涉及产品" prop="relatedProducts">
        <el-select
          v-model="queryParams.relatedProducts"
          filterable
          placeholder="请选择"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="牌号" prop="brandNumber">
        <el-input
          v-model="queryParams.brandNumber"
          placeholder="请输入牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="施胶部位" prop="gluePosition">
        <el-input
          v-model="queryParams.gluePosition"
          placeholder="请输入施胶部位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="基材材质" prop="baseMaterial">
        <el-input
          v-model="queryParams.baseMaterial"
          placeholder="请输入基材材质"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产日期" prop="productionDate">
        <el-date-picker
          clearable
          v-model="queryParams.productionDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择生产日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="有效日期截止" prop="expirationDate">
        <el-date-picker
          clearable
          v-model="queryParams.expirationDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择有效日期截止"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="过期判断" prop="isExpired">
        <el-input
          v-model="queryParams.isExpired"
          placeholder="请输入过期判断"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类编号" prop="classifyId">
        <el-input
          v-model="queryParams.classifyId"
          placeholder="请输入分类编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类名称" prop="classifyName">
        <el-input
          v-model="queryParams.classifyName"
          placeholder="请输入分类名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:splice:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:splice:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:splice:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          type="primary"
          @click="handleExport"
          v-hasPermi="['system:splice:export']"
          >下载</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-upload2"
          size="mini"
          v-hasPermi="['system:splice:import']"
          @click="handleImport"
          >导入</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="spliceList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
      <el-table-column label="牌号" align="center" prop="brandNumber" />
      <el-table-column label="涉及产品" align="center" prop="relatedProducts" />
      <el-table-column label="施胶部位" align="center" prop="gluePosition" />
      <el-table-column
        label="施胶部位图片"
        align="center"
        prop="gluePositionImage"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.gluePositionImage"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>

      <el-table-column label="基材材质" align="center" prop="baseMaterial" />
      <el-table-column label="性能" align="center" prop="fixingMethod" />
      <el-table-column label="作用" align="center" prop="purpose" />
      <el-table-column label="施胶环境" align="center" prop="glueEnvironment" />
      <el-table-column
        label="储存环境"
        align="center"
        prop="storageEnvironment"
      />
      <el-table-column label="过期判断" align="center" prop="isExpired" />
      <el-table-column label="备注" align="center" prop="remark" />

      <!-- <el-table-column
        label="生产日期"
        align="center"
        prop="productionDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.productionDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="有效日期截止"
        align="center"
        prop="expirationDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expirationDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分类编号" align="center" prop="classifyId" />
      <el-table-column label="分类名称" align="center" prop="classifyName" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:splice:edit']"
            >修改</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:splice:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改粘接接头信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="牌号" prop="brandNumber">
              <el-input v-model="form.brandNumber" placeholder="请输入牌号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="涉及产品" prop="relatedProducts">
              <el-input
                v-model="form.relatedProducts"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="施胶部位" prop="gluePosition">
              <el-input
                v-model="form.gluePosition"
                placeholder="请输入施胶部位"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="基材材质" prop="baseMaterial">
              <el-input
                v-model="form.baseMaterial"
                placeholder="请输入基材材质"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性能" prop="fixingMethod">
              <el-input v-model="form.fixingMethod" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="作用" prop="purpose">
              <el-input v-model="form.purpose" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="施胶环境" prop="glueEnvironment">
              <el-input
                v-model="form.glueEnvironment"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="储存环境" prop="storageEnvironment">
              <el-input
                v-model="form.storageEnvironment"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="过期判断" prop="isExpired">
              <el-input v-model="form.isExpired" placeholder="请输入过期判断" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="施胶部位图片" prop="gluePositionImage">
              <image-upload v-model="form.gluePositionImage" />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- <el-form-item label="生产日期" prop="productionDate">
          <el-date-picker
            clearable
            v-model="form.productionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生产日期"
          >
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="有效日期截止" prop="expirationDate">
          <el-date-picker
            clearable
            v-model="form.expirationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择有效日期截止"
          >
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
            <el-checkbox
              v-model="upload.updateSupport"
            />是否更新已经存在的用户数据
          </div> -->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSplice,
  getSplice,
  delSplice,
  addSplice,
  updateSplice,
  getModel,
} from "@/api/glue/splice";
import { getToken } from "@/utils/auth";

export default {
  name: "Splice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 粘接接头信息表格数据
      spliceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        brandNumber: null,
        relatedProducts: null,
        gluePosition: null,
        gluePositionImage: null,
        baseMaterial: null,
        fixingMethod: null,
        purpose: null,
        glueEnvironment: null,
        storageEnvironment: null,
        productionDate: null,
        expirationDate: null,
        isExpired: null,
        classifyId: null,
        classifyName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/splice/import",
      },
      options: [],
    };
  },
  created() {
    this.getList();
    this.getTypeList();
  },
  methods: {
    /** 查询粘接接头信息列表 */
    getList() {
      this.loading = true;
      listSplice(this.queryParams).then((response) => {
        this.spliceList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.getTypeList();
      });
    },

    getTypeList() {
      getModel().then((response) => {
        this.options = [];
        response.data.forEach((element) => {
          this.options.push({
            label: element,
            value: element,
          });
        });
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        brandNumber: null,
        relatedProducts: null,
        gluePosition: null,
        gluePositionImage: null,
        baseMaterial: null,
        fixingMethod: null,
        purpose: null,
        glueEnvironment: null,
        storageEnvironment: null,
        productionDate: null,
        expirationDate: null,
        isExpired: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        classifyId: null,
        classifyName: null,
      };
      this.resetForm("form");
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "粘接接头信息导入";
      this.upload.open = true;
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },

    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },

    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "/system/splice/exportTemplate",
        {},
        `粘接接头导入模板_${new Date().getTime()}.xlsx`
      );
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加粘接接头信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSplice(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改粘接接头信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateSplice(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSplice(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除粘接接头信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSplice(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/splice/export",
        {},
        `粘接接头信息列表_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
